﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLMS.DAL;
using OLMS.BLL;

namespace OLMS
{
    public partial class Order : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnOrderShow.CssClass = "button-default pure-button pure-button-primary";
                OrderBinder(false);
            }
        }
        private void OrderBinder(bool isHistrory)
        {
            string[] orderStatusList = new string[]
            {
                "未付款","待发货","已发货","确认收货","退货"
            };
            Users user = Session["user"] as Users;
            if (user != null)
            {
                List<Orders> list = OrderBLL.GetOrdersList(user.Phone);
                if (isHistrory)
                {
                    //显示历史订单
                    list = list.Where(o => o.OrderStatus == 3 || o.OrderStatus == 4).ToList();
                }
                else
                {
                    //显示正在处理订单
                    list = list.Where(o => o.OrderStatus < 3).ToList();
                }
                repOrdersList.DataSource = list.Select(o => new
                {
                    OrderId = o.OrderId,
                    Username = o.Username,
                    Phone = o.Phone,
                    Address = o.Address,
                    Email = o.Email,
                    PostalCode = o.PostalCode,
                    OrderDate = o.OrderDate,
                    Total = o.Total,
                    OrderStatus = o.OrderStatus,
                    OrderStatusName = orderStatusList[(int)o.OrderStatus]
                }).ToList();
                repOrdersList.DataBind();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
        //确认收货
        protected void btnComplate_Command(object sender, CommandEventArgs e)
        {
           
            int orderId = Convert.ToInt32(e.CommandArgument);
            OrderBLL.UpdateOrderStatus(orderId, 3);
            OrderBinder(false);
        }
        //显示订单详情
        protected void btnDetail_Command(object sender, CommandEventArgs e)
        {
            int orderId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("OrderItem.aspx?oid=" + orderId);
        }
        //退货
        protected void btnBack_Command(object sender, CommandEventArgs e)
        {          
            int orderId = Convert.ToInt32(e.CommandArgument);
            OrderBLL.UpdateOrderStatus(orderId, 4);
            OrderBinder(false);
        }

        protected void btnOrderShow_Click(object sender, EventArgs e)
        {
            //显示正在处理订单
            btnOrderShow.CssClass = "button-default pure-button pure-button-primary";
            btnOrderHistrory.CssClass = "button-default pure-button";
            OrderBinder(false);
        }

        protected void btnOrderHistrory_Click(object sender, EventArgs e)
        {
            //显示历史订单
            btnOrderShow.CssClass = "button-default pure-button";
            btnOrderHistrory.CssClass = "button-default pure-button pure-button-primary";
            OrderBinder(true);
        }
        //更改密码
        protected void btnChgPwd_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChangePassword.aspx");
        }
    }
}